import {Toast, MessageBox} from 'mint-ui';
/**
 * @module common/mintUI
 * @author ashen
 * @description  调用mintUI组件的各个方法
 */
export default {
  /** @function
   *  @param  {String} mes - toast的消息
   */
  openToast (mes) {
    Toast({
      message: mes,
      position: 'middle',
      duration: 1000
    });
  },
  closeMessage () {
    MessageBox.close();
  },
  /** @function
   *  @param  {String} message - alert的消息
   */
  openAlert (message) {
    return new Promise((resolve, reject) => {
      MessageBox.alert(message || '操作成功!', '提示').then(action => {
        resolve(action);
      }).catch(error => {
        console.log(error);
      });
    });
  },
  /** @function
   *  @param  {String} message - confirm的消息
   */
  openConfirm (message, title) {
    return new Promise((resolve) => {
      MessageBox.confirm(message || '确定执行此操作?', title || '提示', '你要取消吗').then(action => {
        if (action === 'confirm') {
          resolve(action);
        }
      }).catch(error => {
        console.log(error);
      });
    });
  },
  /** @function
   *  @param  {Object} {'警告', '确认删除吗？', true, true, '删除', '取消删除'}
   */
  openSpeConfirm (title, message, showConfirmButton, showCancelButton, confirmButtonText, cancelButtonText) {
    return new Promise((resolve) => {
      MessageBox({
        title: title,
        message: message,
        showConfirmButton: showConfirmButton,
        showCancelButton: showCancelButton,
        confirmButtonText: confirmButtonText,
        cancelButtonText: cancelButtonText,
        confirm: function () {
        }
      }).then(action => {
        if (action === 'confirm') {
          resolve(action);
        }
      }).catch(error => {
        console.log(error);
      });
    });
  },
  /** @function
   *  @param  {String} message - Prompt的提示
   */
  openPrompt (msg, option) {
    return new Promise((resolve, reject) => {
      MessageBox.prompt(msg, option).then(({value}) => {
        if (value) {
          resolve(value);
        }
      }).catch(error => {
        reject(error);
      });
    });
  },
  /** @function
   *  @param  {String} div - 显示的自定义template片段
   */
  openModal (div) {
    return new Promise((resolve) => {
      MessageBox.prompt(div, '', {showInput: false}).then(({value}) => {
        if (value) {
          resolve(value);
        }
      }).catch(error => {
        console.log(error);
      });
    });
  }
};
